package com.wtwd.campus.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.campus.dao.LocationSubmeterMapper;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.LocationSubmeter;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.LocationSubmeterService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author mjy
 * @date 2022/11/24
 * @description
 */
@Service
public class LocationSubmeterServiceImpl extends ServiceImpl<LocationSubmeterMapper, LocationSubmeter> implements LocationSubmeterService {


    @Resource
    private LocationSubmeterMapper locationSubmeterMapper;

    @Resource
    private DeviceInfoService deviceInfoService;

    //根据imei最后一个决定存储库表名
    @Override
    public boolean save(LocationSubmeter locationSubmeter, String imei) {
        if (StringUtils.isBlank(imei) || locationSubmeter == null) {
            return false;
        }
        String tableName = getTableName(imei);
        locationSubmeter.setCreateTime(new Date().getTime());
        locationSubmeter.setUpdateTime(new Date().getTime());
        Integer rest = locationSubmeterMapper.save(tableName, locationSubmeter);
        return rest > 0 ? true : false;
    }

    @Override
    public String getTableName(String imei) {
        if (StringUtils.isBlank(imei)) {
            return null;
        }
        return TABLE_NAME.concat(imei.substring(imei.length() - 1));
    }

    @Override
    public List<LocationSubmeter> queryLocationSubmeterByDeviceIdAndTime(String tableName, Integer deviceId, long startTime, long endTime) {
        if (deviceId == null) {
            return null;
        }
        DeviceInfo device = deviceInfoService.getDeviceInfoByDeviceId(deviceId);
        if (device == null) {
            return null;
        }
        return locationSubmeterMapper.queryLocationSubmeterByDeviceIdAndTime(tableName, deviceId, startTime, endTime);
    }

    @Override
    public IPage<LocationSubmeter>  queryDeviceLocationPage(String tableName, Integer deviceId, Page<LocationSubmeter> page) {
        if (deviceId == null) {
            return null;
        }
        return locationSubmeterMapper.queryDeviceLocationPage(tableName, deviceId, page);
    }

    @Override
    public List<LocationSubmeter> queryLocationSubmeterByImeiAndTime(String imei, long startTime, long endTime) {
        if (StringUtils.isBlank(imei)) {
            return null;
        }
        DeviceInfo device = deviceInfoService.getDeviceByImei(imei);
        if (device == null) {
            return null;
        }
        return queryLocationSubmeterByDeviceIdAndTime(getTableName(imei), device.getDeviceId(), startTime, endTime);
    }

    @Override
    public boolean clearLocationData(long time) {
        int rest = 0;
        for (int i = 0; i < 9; i++) {
            rest += locationSubmeterMapper.clearLocationDataByTimeAndTableName(time, TABLE_NAME + i);
        }
        return rest > 0 ? true : false;
    }

    @Override
    public IPage<LocationSubmeter> queryDeviceLocationPageByImei(String imei, Page<LocationSubmeter> page) {
        if (StringUtils.isBlank(imei)) {
            return null;
        }
        DeviceInfo device = deviceInfoService.getDeviceByImei(imei);
        if (device == null) {
            return null;
        }
        return queryDeviceLocationPage(getTableName(imei),device.getDeviceId(),page);
    }

}
